﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Autofac;
using BCVP.Net8.IService;
using BCVP.Net8.Repository;
using BCVP.Net8.Service;

namespace BCVP.Net8.Extension
{
  public class AutofacModuleRegister : Autofac.Module
  {
    protected override void Load(ContainerBuilder builder)
    {
      var basePath = AppContext.BaseDirectory;
      var servicesDllFile = Path.Combine(basePath, "BCVP.Net8.Service.dll");
      var repositoryDllFile = Path.Combine(basePath, "BCVP.Net8.Repository.dll");

      builder.RegisterGeneric(typeof(BaseRepository<>)).As(typeof(IBaseRepository<>)).InstancePerDependency();//注册仓储
      //builder.RegisterGeneric(typeof(BaseService<,>)).As(typeof(IBaseService<,>)).InstancePerDependency();//注册服务

      // 获取 Service.dll 程序集服务，并注册
      var assemblyServices = Assembly.LoadFrom(servicesDllFile);
      builder.RegisterAssemblyTypes(assemblyServices).AsImplementedInterfaces()
        .InstancePerDependency()
        .InstancePerDependency()
        .PropertiesAutowired();
      // 获取 Repository.dll 程序集服务，并注册
      var assemblyRepository = Assembly.LoadFrom(repositoryDllFile);
      builder.RegisterAssemblyTypes(assemblyRepository).AsImplementedInterfaces()
        .InstancePerDependency()
        .InstancePerDependency()
        .PropertiesAutowired();
    }
  }
}
